home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / prog / nextgo23.taz / nextgo23 / NeXTGo / GoApp.m < prev    next >
Encoding:
Text File  |  1993-02-09  |  28.5 KB  |  1,334 lines

  1. #include "comment.header"
  2.  
  3. /* Generated by Interface Builder */
  4.  
  5. #import <appkit/appkit.h>
  6. #import <appkit/PrintInfo.h>
  7.  
  8. #include <string.h>
  9. #import "GoApp.h"
  10. #import "Board.h"
  11. #import "ClickCell.h"
  12. #include "igs.h"
  13. #include "godict.h"
  14.  
  15. #define CURRENT_VERSION    "Version 2.3"
  16.  
  17. unsigned char p[19][19], l[19][19], ma[19][19], ml[19][19];
  18. char special_characters[19][19], sgComment[2000], sgNodeName[200];
  19. int hist[19][19], currentMoveNumber;
  20. int rd, lib, play, pass, handicap, whiteSide, blackSide, MAXX, MAXY;
  21. int currentStone, opposingStone, blackCaptured, whiteCaptured;
  22. int blackTerritory, whiteTerritory, SmartGoGameFlag, initialization;
  23. float black_Score, white_Score;
  24. int blackCapturedKoI, blackCapturedKoJ, whiteCapturedKoI, whiteCapturedKoJ;
  25. int bothSides, neitherSide, blackPassed, whitePassed, manualScoring, manScoreTemp;
  26. int typeOfScoring, AGAScoring;
  27. int opn[9];                               // opening pattern flag
  28. int gameType;
  29. BOOL finished, printBold;
  30. gameHistory gameMoves[500];
  31. int lastMove;
  32. node *SGgameMoves, *currentNode, *rootNode;
  33. FILE *smartGoInputFile;
  34. char *SGfile, *currentSGfilePosition;
  35. char *IGSStatusText, IGSPasswordText[20], IGSLoginText[20];
  36. GODICT *godict;
  37.  
  38. void getAppDirectory(char *appDirectory)
  39. {
  40.   FILE *process;
  41.   char command[256];
  42.   char *suffix;
  43.  
  44.   strcpy(appDirectory, NXArgv[0]);
  45.   if (appDirectory[0] == '/')
  46.     {
  47.       if (suffix = rindex(appDirectory,'/'))
  48.     *suffix = '\0';
  49.     }
  50.   else
  51.     {
  52.       sprintf(command, "which '%s'\n", NXArgv[0]);
  53.       process = popen(command,"r");
  54.       fscanf(process, "%s", appDirectory);
  55.       pclose(process);
  56.       if (suffix = rindex(appDirectory, '/'))
  57.     *suffix = '\0';
  58.       chdir(appDirectory);
  59.       getwd(appDirectory);
  60.     }
  61. }
  62.  
  63. char *getpassword(void)
  64. {
  65.   return IGSPasswordText;
  66. }
  67.  
  68. char *getloginname(void)
  69. {
  70.   return IGSLoginText;
  71. }
  72.  
  73. void checkingNetTraffic(DPSTimedEntry timedEntry, double timeNow, void *data)
  74. {
  75.   [(id)data checkNetTraffic];
  76. }
  77.  
  78. void IGSSendString(char *s)
  79. {
  80.   sendstr(s);
  81.  
  82.   printBold = YES;
  83.   [NXApp SetIGSStatus:s];
  84. }
  85.  
  86. void stripBrackets(char s[])
  87. {
  88.   int i, j;
  89.  
  90.   i = 0;
  91.   while (i < strlen(s))
  92.     {
  93.       if ((s[i] == '[') || (s[i] == ']'))
  94.     {
  95.       for (j = i; j < strlen(s) - 1; j++)
  96.         s[j] = s[j+1];
  97.       s[strlen(s) - 1] = 0;
  98.     }
  99.       else
  100.     {
  101.       i++;
  102.     }
  103.     }
  104. }
  105.  
  106. int saveNeXTGoFile(const char *fileName)
  107. {
  108.   FILE *NGoFile;
  109.   int i, j;
  110.  
  111.   if ((NGoFile = fopen(fileName, "w")) == NULL)
  112.     return 1;
  113.  
  114.   fprintf(NGoFile, "%d\n", handicap);
  115.   fprintf(NGoFile, "%d\n", whiteSide);
  116.   fprintf(NGoFile, "%d\n", blackSide);
  117.   fprintf(NGoFile, "%d\n", MAXX);
  118.   fprintf(NGoFile, "%d\n", MAXY);
  119.   fprintf(NGoFile, "%d\n", currentStone);
  120.   fprintf(NGoFile, "%d\n", opposingStone);
  121.   fprintf(NGoFile, "%d\n", blackCaptured);
  122.   fprintf(NGoFile, "%d\n", whiteCaptured);
  123.   fprintf(NGoFile, "%d\n", blackCapturedKoI);
  124.   fprintf(NGoFile, "%d\n", blackCapturedKoJ);
  125.   fprintf(NGoFile, "%d\n", whiteCapturedKoI);
  126.   fprintf(NGoFile, "%d\n", whiteCapturedKoJ);
  127.   fprintf(NGoFile, "%d\n", bothSides);
  128.   fprintf(NGoFile, "%d\n", neitherSide);
  129.   fprintf(NGoFile, "%d\n", blackPassed);
  130.   fprintf(NGoFile, "%d\n", whitePassed);
  131.   for (i = 0; i < 9; i++)
  132.     fprintf(NGoFile, "%d\n", opn[i]);
  133.   for (i = 0; i < MAXX; i++)
  134.     for (j = 0; j < MAXY; j++)
  135.       fprintf(NGoFile, "%d\n", p[i][j]);
  136.       
  137.   fclose(NGoFile);
  138.  
  139.   return 0;
  140. }
  141.  
  142. int saveSmartGoFile(const char *fileName)
  143. {
  144.   FILE *NGoFile;
  145.   int i;
  146.  
  147.   if ((NGoFile = fopen(fileName, "w")) == NULL)
  148.     return 1;
  149.   fprintf(NGoFile, "(\n;\nGaMe[1]\nVieW[]\n");
  150.   fprintf(NGoFile, "SiZe[%d]\nKoMi[%3.1f]\nHAndicap[%d]", MAXX, 
  151.       ((handicap == 0)?KOMI:0.5), handicap);
  152.   fprintf(NGoFile,"\nComment[ A game between ");
  153.  
  154.   if (gameType != LOCAL)
  155.     {
  156.       fprintf(NGoFile, "two people on the network.\n\n");
  157.     }
  158.   else if (neitherSide)
  159.     {
  160.       fprintf(NGoFile,"two human players.\n\n");
  161.     }
  162.   else if (bothSides)
  163.     {
  164.       fprintf(NGoFile,"two computer players.\n\n");
  165.     }
  166.   else
  167.     {
  168.       fprintf(NGoFile,"the computer and a human player.\n\n");
  169.     }
  170.  
  171.   if (finished)
  172.     {
  173.       fprintf(NGoFile,"        Result:  %s wins by %8.1f.]\n",
  174.           (black_Score > white_Score)?"Black":"White",
  175.           fabs(black_Score - white_Score));
  176.     }
  177.   else
  178.     {
  179.       fprintf(NGoFile, "]\n");
  180.     }
  181.  
  182.   if (handicap > 1)
  183.     {
  184.       int q, half;
  185.  
  186.       q = (MAXX < 13)?2:3;
  187.       half = (MAXX + 1)/2 - 1;
  188.  
  189.       switch (handicap) {
  190.       case 2:
  191.     fprintf(NGoFile, "AddBlack[%c%c][%c%c]\n", q+'a', MAXY-q-1+'a',
  192.         MAXX-q-1+'a', q+'a');
  193.     break;
  194.       case 3:
  195.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c]\n", q+'a', MAXY-q-1+'a',
  196.         MAXX-q-1+'a', q+'a', q+'a', q+'a');
  197.     break;
  198.       case 4:
  199.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c][%c%c]\n", q+'a',
  200.         MAXY-q-1+'a', MAXX-q-1+'a', q+'a', q+'a', q+'a', MAXX-q-1+'a',
  201.         MAXY-q-1+'a');
  202.     break;
  203.       case 5:
  204.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c][%c%c][%c%c]\n", q+'a',
  205.         MAXY-q-1+'a', MAXX-q-1+'a', q+'a', q+'a', q+'a', MAXX-q-1+'a',
  206.         MAXY-q-1+'a', half+'a', half+'a');
  207.     break;
  208.       case 6:
  209.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c][%c%c][%c%c][%c%c]\n",
  210.         q+'a', MAXY-q-1+'a', MAXX-q-1+'a', q+'a', q+'a', q+'a',
  211.         MAXX-q-1+'a', MAXY-q-1+'a', q+'a', half+'a', MAXX-q-1+'a',
  212.         half+'a');
  213.     break;
  214.       case 7:
  215.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c][%c%c][%c%c][%c%c][%c%c]\n",
  216.         q+'a', MAXY-q-1+'a', MAXX-q-1+'a', q+'a', q+'a', q+'a',
  217.         MAXX-q-1+'a', MAXY-q-1+'a', q+'a', half+'a', MAXX-q-1+'a',
  218.         half+'a', half+'a', half+'a');
  219.     break;
  220.       case 8:
  221.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c][%c%c][%c%c][%c%c][%c%c][%c%c]\n",
  222.         q+'a', MAXY-q-1+'a', MAXX-q-1+'a', q+'a', q+'a', q+'a',
  223.         MAXX-q-1+'a', MAXY-q-1+'a', q+'a', half+'a', MAXX-q-1+'a',
  224.         half+'a', half+'a', q+'a', half+'a', MAXY-q-1+'a');
  225.     break;
  226.       case 9:
  227.     fprintf(NGoFile, "AddBlack[%c%c][%c%c][%c%c][%c%c][%c%c][%c%c][%c%c][%c%c][%c%c]\n",
  228.         q+'a', MAXY-q-1+'a', MAXX-q-1+'a', q+'a', q+'a', q+'a',
  229.         MAXX-q-1+'a', MAXY-q-1+'a', q+'a', half+'a', MAXX-q-1+'a',
  230.         half+'a', half+'a', q+'a', half+'a', MAXY-q-1+'a', half+'a',
  231.         half+'a');
  232.     break;
  233.       }
  234.     }
  235.     
  236.   for (i = 0; i < lastMove; i++)
  237.     {
  238.       switch (gameMoves[i].color)
  239.     {
  240.     case 1:
  241.       if (gameMoves[i].x >= 0)
  242.         {
  243.           fprintf(NGoFile,";\nWhite[%c%c]\n", gameMoves[i].x+97,
  244.               gameMoves[i].y+97);
  245.         }
  246.       else
  247.         {
  248.           fprintf(NGoFile, ";\nWhite[tt]\n");
  249.         }
  250.       break;
  251.     case 2:
  252.       if (gameMoves[i].x >= 0)
  253.         {
  254.           fprintf(NGoFile,";\nBlack[%c%c]\n", gameMoves[i].x+97,
  255.               gameMoves[i].y+97);
  256.         }
  257.       else
  258.         {
  259.           fprintf(NGoFile, ";\nBlack[tt]\n");
  260.         }
  261.       break;
  262.     }
  263.     }
  264.  
  265.   fprintf(NGoFile,")\n\n");
  266.   fclose(NGoFile);
  267.  
  268.   return 0;
  269. }
  270.  
  271. @implementation GoApp
  272.  
  273. - appDidInit:app 
  274. {
  275.   FILE *NeXTGoPrefs;
  276.   char str[80];
  277.   
  278.   godict = NULL;
  279.   IGSfont = [Font newFont:"Ohlfs" size:10];
  280.   IGSboldFont = [Font newFont:"Helvetica" size:10];
  281.   [[MainGoView window] setMiniwindowIcon:"NeXTGoFile"];
  282.   strcpy(str, "NeXTGo -- ");
  283.   strcat(str, CURRENT_VERSION);
  284.   [versionString setStringValue:CURRENT_VERSION];
  285.   [[MainGoView window] setTitle:str];
  286.   [[IGSStatus docView] setFont:IGSfont];
  287.   [[smartGoComments docView] setFont:IGSfont];
  288.   
  289.   lastMove = 0;
  290.   gameType = LOCAL;
  291.  
  292.   strcpy(str, NXHomeDirectory());
  293.   strcat(str, "/.NeXTGoPrefs");
  294.  
  295.   if ((NeXTGoPrefs = fopen(str, "r")) != NULL)
  296.     {
  297.       fscanf(NeXTGoPrefs, "%d", &handicap);
  298.       fscanf(NeXTGoPrefs, "%d", &MAXX);
  299.       fscanf(NeXTGoPrefs, "%d", &blackSide);
  300.       fscanf(NeXTGoPrefs, "%d", &whiteSide);
  301.       fscanf(NeXTGoPrefs, "%d", &manualScoring);
  302.       fscanf(NeXTGoPrefs, "%d", &typeOfScoring);
  303.       fscanf(NeXTGoPrefs, "%d", &AGAScoring);
  304.       fscanf(NeXTGoPrefs, "%s", servename);
  305.       fscanf(NeXTGoPrefs, "%s", IGSPasswordText);
  306.       fscanf(NeXTGoPrefs, "%s", IGSLoginText);
  307.       fclose(NeXTGoPrefs);
  308.       
  309.       MAXY = MAXX;
  310.       manScoreTemp = manualScoring;
  311.  
  312.       [handicapSlider setIntValue:handicap];
  313.       [handicapText setIntValue:handicap];
  314.       [sizeSlider setIntValue:MAXX];
  315.       [sizeText setIntValue:MAXX];
  316.       [BlackPlayer selectCellAt:blackSide:0];
  317.       [WhitePlayer selectCellAt:whiteSide:0];
  318.       [scoringMethod selectCellAt:manualScoring:0];
  319.       [scoringType selectCellAt:typeOfScoring:0];
  320.       [AGAscoringMethodFlag setIntValue:AGAScoring];
  321.       [savePrefsFlag setIntValue:1];
  322.       [IGSAddress setStringValue:servename];
  323.       [IGSLoginName setStringValue:IGSLoginText];
  324.       [IGSPassword setStringValue:IGSPasswordText];
  325.     }
  326.     
  327.   neitherSide = 0;
  328.   bothSides = 0;
  329.   manScoreTemp = manualScoring;
  330.     
  331.   if ((blackSide == 0) && (whiteSide == 0))
  332.     neitherSide++;
  333.   if ((blackSide == 1) && (whiteSide == 1))
  334.     bothSides++;
  335.     
  336.   [self NewGame:self];
  337.  
  338.   [[[NXApp printInfo] setHorizCentered:YES] setVertCentered:YES];
  339.  
  340.   openReq = [OpenPanel new];
  341.   saveReq = [SavePanel new];
  342.  
  343.   SmartGoGameFlag = 0;
  344.   
  345.   return self;
  346. }
  347.  
  348. - showError: (const char *)errorMessage
  349. {
  350.   NXRunAlertPanel("NeXTGo Error", errorMessage,"OK", NULL, NULL);
  351.   return self;
  352. }
  353.  
  354. - UserPass:sender
  355. {
  356.   if (SmartGoGameFlag)
  357.     return self;
  358.  
  359.   [MainGoView passMove];
  360.  
  361.   return self;
  362. }
  363.  
  364. - stopGame:sender
  365. {
  366.   if (SmartGoGameFlag)
  367.     return self;
  368.  
  369.   [MainGoView stop:self];
  370.     
  371.   return self;
  372. }
  373.  
  374. - startGame:sender
  375. {
  376.   if (SmartGoGameFlag)
  377.     return self;
  378.  
  379.   [MainGoView go:self];
  380.  
  381.   return self;
  382. }
  383.  
  384. - NewGame:sender
  385. {
  386.   int resp;
  387.  
  388.   if (gameType == IGSGAME)
  389.     {
  390.       NXRunAlertPanel("IGS Error", "You must first close your IGS session before\n\
  391. beginning a new game.", "OK", NULL, NULL);
  392.       return self;
  393.     }
  394.  
  395.   if (SmartGoGameFlag)
  396.     {
  397.       SmartGoGameFlag = 0;
  398.       [smartGoPanel close];
  399.     }
  400.     
  401.   gameType = LOCAL;
  402.  
  403.   if ((lastMove > 0) && (!finished))
  404.     {
  405.       resp = NXRunAlertPanel("NeXTGo Warning", "A game is in process.  Do you wish to abandon it?", "Abandon", "Cancel", 0);
  406.  
  407.       if (resp == NX_ALERTALTERNATE)
  408.     return self;
  409.     }
  410.  
  411.   [MainGoView startNewGame];
  412.   [MainGoView display];
  413.  
  414.   lastMove = 0;
  415.  
  416.   return self;
  417. }
  418.  
  419. - SetPreferences:sender
  420. {
  421.   FILE *NeXTGoPrefs;
  422.   char str[80];
  423.  
  424.   handicap = [handicapSlider intValue];
  425.   MAXX = MAXY = [sizeSlider intValue];
  426.   blackSide = [BlackPlayer selectedRow];
  427.   whiteSide = [WhitePlayer selectedRow];
  428.   manualScoring = [scoringMethod selectedRow];
  429.   typeOfScoring = [scoringType selectedRow];
  430.   AGAScoring = [AGAscoringMethodFlag intValue];
  431.   strcpy(servename, (char *)[IGSAddress stringValue]);
  432.   strcpy(IGSPasswordText, (char *)[IGSPassword stringValue]);
  433.   strcpy(IGSLoginText, (char *)[IGSLoginName stringValue]);
  434.     
  435.   neitherSide = 0;
  436.   bothSides = 0;
  437.   manScoreTemp = manualScoring;
  438.     
  439.   if ((blackSide == 0) && (whiteSide == 0))
  440.     neitherSide++;
  441.   if ((blackSide == 1) && (whiteSide == 1))
  442.     bothSides++;
  443.     
  444.   [self NewGame:self];
  445.     
  446.   [prefPanel close];
  447.   
  448.   if ([savePrefsFlag intValue])
  449.   {
  450.     strcpy(str, NXHomeDirectory());
  451.     strcat(str, "/.NeXTGoPrefs");
  452.     if ((NeXTGoPrefs = fopen(str, "w")) == NULL)
  453.       {
  454.         NXRunAlertPanel("NeXTGo Error", "I cannot write to the preferences file.",
  455.             "OK", 0, 0);
  456.         [self NewGame:self];
  457.         return self;
  458.       }
  459.     fprintf(NeXTGoPrefs, "%d  ", handicap);
  460.     fprintf(NeXTGoPrefs, "%d  ", MAXX);
  461.     fprintf(NeXTGoPrefs, "%d  ", blackSide);
  462.     fprintf(NeXTGoPrefs, "%d  ", whiteSide);
  463.     fprintf(NeXTGoPrefs, "%d  ", manualScoring);
  464.     fprintf(NeXTGoPrefs, "%d  ", typeOfScoring);
  465.     fprintf(NeXTGoPrefs, "%d  ", AGAScoring);
  466.     fprintf(NeXTGoPrefs, "%s  ", servename);
  467.     fprintf(NeXTGoPrefs, "%s  ", IGSPasswordText);
  468.     fprintf(NeXTGoPrefs, "%s\n\n", IGSLoginText);
  469.     fclose(NeXTGoPrefs);
  470.   }
  471.  
  472.   return self;
  473. }
  474.  
  475. - displayNewSGNode
  476. {
  477.   node *var_list, *current_var;
  478.  
  479.   [smartGoNodeNumber setIntValue:currentNode->nodenum];
  480.   [[smartGoComments docView] setText:""];
  481.   [[smartgoVariants docView] setText:""];
  482.   [self AddSGComment:sgComment];
  483.   [self SetSGNodeName:sgNodeName];
  484.  
  485.   [MainGoView display];
  486.   [MainGoView setblacksPrisoners:blackCaptured];
  487.   [MainGoView setwhitesPrisoners:whiteCaptured];
  488.  
  489.   for (var_list = currentNode->variants; var_list != NULL;
  490.        var_list = var_list->next_var)
  491.     {
  492.       char var_name[80];
  493.  
  494.       current_var = var_list;
  495.       while (current_var->properties == NULL)
  496.     {
  497.       current_var = forwardOneNode(current_var);
  498.     }
  499.  
  500.       sgNodeName[0] = 0;
  501.       buildToNode(current_var);
  502.       sprintf(var_name, "%d: %s", current_var->nodenum, sgNodeName);
  503.       [self AddSGVariantName:var_name];
  504.     }
  505.  
  506.   if (currentNode->variants != NULL)
  507.     {
  508.       sgNodeName[0] = 0;
  509.       sgComment[0] = 0;
  510.       buildToNode(currentNode);
  511.     }
  512.  
  513.   return self;
  514. }
  515.  
  516. - stepSmartGoFile:sender
  517. {
  518.   sgComment[0] = 0;
  519.   sgNodeName[0] = 0;
  520.  
  521.   currentNode = stepForward(currentNode);
  522.  
  523.   [self displayNewSGNode];
  524.  
  525.   return self;
  526. }
  527.  
  528. - stepBackSmartGoFile:sender
  529. {
  530.   sgComment[0] = 0;
  531.   sgNodeName[0] = 0;
  532.  
  533.   currentNode = stepBackward(currentNode);
  534.  
  535.   [self displayNewSGNode];
  536.  
  537.   return self;
  538. }
  539.  
  540. - jumpSmartGoFile:sender
  541. {
  542.   sgComment[0] = 0;
  543.   sgNodeName[0] = 0;
  544.  
  545.   currentNode = jumpForward(currentNode);
  546.  
  547.   [self displayNewSGNode];
  548.  
  549.   return self;
  550. }
  551.  
  552. - jumpBackSmartGoFile:sender
  553. {
  554.   sgComment[0] = 0;
  555.   sgNodeName[0] = 0;
  556.  
  557.   currentNode = jumpBackward(currentNode);
  558.  
  559.   [self displayNewSGNode];
  560.  
  561.   return self;
  562. }
  563.  
  564. - AddSGComment:(char *)s
  565. {
  566.   Text *docView = [smartGoComments docView];
  567.   int dvLen;
  568.   char retstr[5];
  569.  
  570.   strcpy(retstr, "\n");
  571.  
  572.   dvLen = [docView textLength];
  573.   [docView setSel:dvLen :dvLen];
  574.   [docView replaceSel: s];
  575.  
  576.   [docView setSel:[docView textLength] :[docView textLength]];
  577.  
  578.   [docView scrollSelToVisible];
  579.  
  580.   return self;
  581. }
  582.  
  583. - AddSGVariantName:(char *)s
  584. {
  585.   Text *docView = [smartgoVariants docView];
  586.   int dvLen;
  587.   char retstr[5];
  588.  
  589.   strcpy(retstr, "\n");
  590.  
  591.   dvLen = [docView textLength];
  592.   [docView setSel:dvLen :dvLen];
  593.   [docView replaceSel: s];
  594.  
  595.   [docView setSel:[docView textLength] :[docView textLength]];
  596.  
  597.   [docView replaceSel: retstr];
  598.   [docView setSel:[docView textLength] :[docView textLength]];
  599.  
  600.   [docView scrollSelToVisible];
  601.  
  602.   return self;
  603. }
  604.  
  605. - SetSGNodeName:(char *)s
  606. {
  607.   [smartGoNodeName setStringValue:s];
  608.   [smartGoNodeName display];
  609.  
  610.   return self;
  611. }
  612.  
  613. - openNeXTGoFileRequest:sender
  614. {
  615.   FILE *NGoFile;
  616.   int i, j, t;
  617.   const char *fileName;
  618.   const char *const types[2] = {"nextgo", NULL};
  619.  
  620.   if ([openReq runModalForTypes:types] && (fileName = [openReq filename])) {
  621.  
  622.     if ((NGoFile = fopen(fileName, "r")) == NULL)
  623.       return self;
  624.  
  625.     fscanf(NGoFile, "%d", &handicap);
  626.     fscanf(NGoFile, "%d", &whiteSide);
  627.     fscanf(NGoFile, "%d", &blackSide);
  628.     fscanf(NGoFile, "%d", &MAXX);
  629.     fscanf(NGoFile, "%d", &MAXY);
  630.     [MainGoView startNewGame];
  631.     fscanf(NGoFile, "%d", &opposingStone);
  632.     fscanf(NGoFile, "%d", ¤tStone);
  633.     fscanf(NGoFile, "%d", &blackCaptured);
  634.     fscanf(NGoFile, "%d", &whiteCaptured);
  635.     fscanf(NGoFile, "%d", &blackCapturedKoI);
  636.     fscanf(NGoFile, "%d", &blackCapturedKoJ);
  637.     fscanf(NGoFile, "%d", &whiteCapturedKoI);
  638.     fscanf(NGoFile, "%d", &whiteCapturedKoJ);
  639.     fscanf(NGoFile, "%d", &bothSides);
  640.     fscanf(NGoFile, "%d", &neitherSide);
  641.     fscanf(NGoFile, "%d", &blackPassed);
  642.     fscanf(NGoFile, "%d", &whitePassed);
  643.     for (i = 0; i < 9; i++)
  644.       {
  645.         fscanf(NGoFile, "%d", &t);
  646.     opn[i] = t;
  647.       }
  648.     for (i = 0; i < MAXX; i++)
  649.       for (j = 0; j < MAXY; j++)
  650.         {
  651.           fscanf(NGoFile, "%d", &t);
  652.       p[i][j] = t;
  653.     }
  654.     [MainGoView updateInfo];
  655.  
  656.     fclose(NGoFile);
  657.     
  658.     [MainGoView lockFocus];
  659.     [[MainGoView window] flushWindow];
  660.     [MainGoView display];
  661.     [MainGoView unlockFocus];    
  662.   } else [self showError:"Error on Open Request"];
  663.  
  664.   NXPing();
  665.  
  666.   return self;
  667. }
  668.  
  669. - initTranslator:sender
  670. {
  671.   char filename[256];
  672.  
  673.   getAppDirectory(filename);
  674.   strcat(filename,"/");
  675.   strcat(filename,DEFDICT);
  676.  
  677.   if (godict == NULL)
  678.     {
  679.       godict = load_dict(filename);
  680.  
  681.       if (godict == NULL)
  682.     {
  683.       NXRunAlertPanel("Translate Error","There is a problem opening the dictionary file.", "OK", 0, 0);
  684.       return self;
  685.     }
  686.     }
  687.  
  688.   [translateWindow makeKeyAndOrderFront:self];
  689.   [translateWindow setMiniwindowIcon:"NeXTGoFile"];
  690.   [translateWindow display];
  691.   [translateTerm setStringValue:""];
  692.   [translateTerm selectText:self];
  693.   [translateButton setEnabled:YES];
  694.   [[translateResults docView] setText:""];
  695.  
  696.   return self;
  697. }
  698.  
  699. - performTranslate:sender
  700. {
  701.   GODICT *d;
  702.   char term[80];
  703.   extern int termtypes, languages;
  704.  
  705.   strcpy(term, [translateTerm stringValue]);
  706.   if (strlen(term) == 0)
  707.     return self;
  708.  
  709.   termtypes = CD_MISC*[transTypeMISC intValue];
  710.   termtypes += CD_NAME*[transTypeNAME intValue];
  711.   termtypes += CD_CHAM*[transTypeCHAM intValue];
  712.   termtypes += CD_TECH*[transTypeTECH intValue];
  713.   termtypes += CD_POLI*[transTypePOLI intValue];
  714.   termtypes += CD_DIGI*[transTypeDIGI intValue];
  715.  
  716.   languages = LANG_DG*[transLangDG intValue];
  717.   languages += LANG_CP*[transLangCP intValue];
  718.   languages += LANG_JP*[transLangJP intValue];
  719.   languages += LANG_CH*[transLangCH intValue];
  720.   languages += LANG_RK*[transLangRK intValue];
  721.   languages += LANG_GB*[transLangGB intValue];
  722.   languages += LANG_NL*[transLangNL intValue];
  723.   languages += LANG_GE*[transLangGE intValue];
  724.   languages += LANG_FR*[transLangFR intValue];
  725.  
  726.   [[translateResults docView] setText:""];
  727.   d = godict;
  728.  
  729.   while (d != NULL)
  730.     {
  731.       d = search_dict(d,term);
  732.       if (d != NULL)
  733.     {
  734.       [self translateOutput:d];
  735.       d = d->dct_next;
  736.     }
  737.     }
  738.   [translateTerm selectText:self];
  739.  
  740.   return self;
  741. }
  742.  
  743. - translateOutput:(GODICT *)d
  744. {
  745.   extern int languages;
  746.  
  747.   printBold = YES;
  748.   [self addTranslateResults:LB_CD];
  749.   switch(d->dct_type)
  750.     {
  751.     case CD_CHAM:
  752.       [self addTranslateResults:MSG_CHAM];
  753.       break;
  754.     case CD_TECH:
  755.       [self addTranslateResults:MSG_TECH];
  756.       break;
  757.     case CD_NAME:
  758.       [self addTranslateResults:MSG_NAME];
  759.       break;
  760.     case CD_POLI:
  761.       [self addTranslateResults:MSG_POLI];
  762.       break;
  763.     case CD_DIGI:
  764.       [self addTranslateResults:MSG_DIGI];
  765.       break;
  766.     default:
  767.       [self addTranslateResults:MSG_MISC];
  768.       break;
  769.     }
  770.  
  771.   [self addTranslateResults:"\n"];
  772.  
  773.   if (d->dct_jp && (languages & (LANG_JP)))
  774.     {
  775.       printBold = YES;
  776.       [self addTranslateResults:LB_JP];
  777.       [self addTranslateResults:d->dct_jp];
  778.       [self addTranslateResults:"\n"];
  779.     }
  780.   if (d->dct_ch && (languages & (LANG_CH)))
  781.     {
  782.       printBold = YES;
  783.       [self addTranslateResults:LB_CH];
  784.       [self addTranslateResults:d->dct_ch];
  785.       [self addTranslateResults:"\n"];
  786.     }
  787.   if (d->dct_rk && (languages & (LANG_RK)))
  788.     {
  789.       printBold = YES;
  790.       [self addTranslateResults:LB_RK];
  791.       [self addTranslateResults:d->dct_rk];
  792.       [self addTranslateResults:"\n"];
  793.     }
  794.   if (d->dct_gb && (languages & (LANG_GB)))
  795.     {
  796.       printBold = YES;
  797.       [self addTranslateResults:LB_GB];
  798.       [self addTranslateResults:d->dct_gb];
  799.       [self addTranslateResults:"\n"];
  800.     }
  801.   if (d->dct_nl && (languages & (LANG_NL)))
  802.     {
  803.       printBold = YES;
  804.       [self addTranslateResults:LB_NL];
  805.       [self addTranslateResults:d->dct_nl];
  806.       [self addTranslateResults:"\n"];
  807.     }
  808.   if (d->dct_ge && (languages & (LANG_GE)))
  809.     {
  810.       printBold = YES;
  811.       [self addTranslateResults:LB_GE];
  812.       [self addTranslateResults:d->dct_ge];
  813.       [self addTranslateResults:"\n"];
  814.     }
  815.   if (d->dct_fr && (languages & (LANG_FR)))
  816.     {
  817.       printBold = YES;
  818.       [self addTranslateResults:LB_FR];
  819.       [self addTranslateResults:d->dct_fr];
  820.       [self addTranslateResults:"\n"];
  821.     }
  822.   if (d->dct_dg && (languages & (LANG_DG)))
  823.     {
  824.       printBold = YES;
  825.       [self addTranslateResults:LB_DG];
  826.       [self addTranslateResults:"\n"];
  827.       [self addTranslateResults:d->dct_dg];
  828.       [self addTranslateResults:"\n"];
  829.     }
  830.   if (d->dct_cp && (languages & (LANG_CP)))
  831.     {
  832.       printBold = YES;
  833.       [self addTranslateResults:LB_CP];
  834.       [self addTranslateResults:d->dct_cp];
  835.       [self addTranslateResults:"\n"];
  836.     }
  837.  
  838.   [self addTranslateResults:"\n\n"];
  839.  
  840.   return self;
  841. }
  842.  
  843. - addTranslateResults:(char *)s
  844. {
  845.   Text *docView = [translateResults docView];
  846.   int dvLen;
  847.  
  848.   dvLen = [docView textLength];
  849.   [docView setSel:dvLen :dvLen];
  850.   [docView setSelFont:IGSfont];
  851.   printBold = NO;
  852.   [docView replaceSel:s];
  853.   [docView setSel:[docView textLength] :[docView textLength]];
  854.  
  855.   [docView scrollSelToVisible];
  856.  
  857.   return self;
  858. }
  859.  
  860. - saveNeXTGoFileRequest:sender
  861. {
  862.   const char *fileName;
  863.   const char *const types[2] = {"nextgo", NULL};
  864.  
  865.   [saveReq setRequiredFileType:types[0]];
  866.  
  867.   if (([saveReq runModalForDirectory:""
  868.       file:""]) && (fileName = [saveReq filename]))
  869.     {
  870.       saveNeXTGoFile(fileName);
  871.     }
  872.     
  873.   NXPing();
  874.   
  875.   return self;
  876. }
  877.  
  878. - openSmartGoFileRequest:sender
  879. {
  880.   const char *fileName;
  881.   const char *const types[2] = {"", NULL};
  882.   char dispFileName[80];
  883.   int i, j;
  884.   struct stat statbuf;
  885.  
  886.   if (gameType != LOCAL)
  887.     {
  888.       NXRunAlertPanel("IGS Error", "You must first close your IGS session before\n\
  889. opening a Smart-Go file.", "OK", NULL, NULL);
  890.       return self;
  891.     }
  892.  
  893.   if ([openReq runModalForTypes:types] && (fileName = [openReq filename])) {
  894.  
  895.     if ((smartGoInputFile = fopen(fileName, "r")) == NULL)
  896.       return self;
  897.  
  898.     SGgameMoves = NULL;
  899.     SmartGoGameFlag = 1;
  900.     initialization = 0;
  901.  
  902.     [smartGoPanel makeKeyAndOrderFront:self];
  903.     [smartGoPanel setMiniwindowIcon:"NeXTGoFile"];
  904.     [smartGoPanel display];
  905.     [stepForwardButton setEnabled:YES];
  906.  
  907.     stat(fileName, &statbuf);
  908.     SGfile = (char *) malloc ((size_t)statbuf.st_size+10);
  909.     if (fread(SGfile, statbuf.st_size, 1, smartGoInputFile) != 1)
  910.       {
  911.     [self showError:"Error on Read"];
  912.     fclose(smartGoInputFile);
  913.     free(SGfile);
  914.  
  915.     return self;
  916.       }
  917.  
  918.     fclose(smartGoInputFile);
  919.  
  920.     [MainGoView startNewGame];
  921.     [MainGoView setMess1:"Smart-Go Playback"];
  922.     j = 0;
  923.     for (i = 0; i < strlen(fileName); i++)
  924.       if (fileName[i] == '/')
  925.     j = i;
  926.     for (i = j+1; i < strlen(fileName); i++)
  927.       dispFileName[i-j-1] = fileName[i];
  928.     dispFileName[strlen(fileName) - j - 1] = 0;
  929.  
  930.     [MainGoView setMess2:dispFileName];
  931.  
  932.     rootNode = parse_tree(SGfile);
  933.     MAXX = MAXY = 19;
  934.     currentNode = stepForward(rootNode);
  935.     [self displayNewSGNode];
  936.   } else
  937.     [self showError:"Error on Open Request"];
  938.  
  939.   NXPing();
  940.  
  941.   return self;
  942. }
  943.  
  944. - saveSmartGoFileRequest:sender
  945. {
  946.   const char *fileName;
  947.   const char *const types[2] = {"mgt", NULL};
  948.  
  949.   [saveReq setRequiredFileType:types[0]];
  950.  
  951.   if (([saveReq runModalForDirectory:""
  952.       file:""]) && (fileName = [saveReq filename]))
  953.     {
  954.       saveSmartGoFile(fileName);
  955.     }
  956.     
  957.   NXPing();
  958.   
  959.   return self;
  960. }
  961.  
  962. - IGSSendCommand:sender
  963. {
  964.   if (strncasecmp((char *)[IGSCommand stringValue], "observe", 7) == 0)
  965.     {
  966.       NXRunAlertPanel("IGS Error", "You must select a game to observe by pressing the observe button.", "OK", 0, 0);
  967.     }
  968.   else
  969.     {
  970.       IGSSendString((char *)[IGSCommand stringValue]);
  971.       IGSSendString("\n");
  972.     }
  973.  
  974.   [IGSCommand setStringValue:""];
  975.   [IGSCommand selectText:self];
  976.  
  977.   return self;
  978. }
  979.  
  980. - IGSConnect:sender
  981. {
  982.   char s[80];
  983.  
  984.   if (strlen((char *)[IGSLoginName stringValue]) == 0)
  985.     {
  986.       NXRunAlertPanel("IGS Error", "You must first set your login name in the Preferences panel.",
  987.               "OK", 0, 0);
  988.       [prefPanel makeKeyAndOrderFront:self];
  989.       [IGSLoginName selectText:self];
  990.  
  991.       return self;
  992.     }
  993.  
  994.   if (strlen((char *)[IGSPassword stringValue]) == 0)
  995.     {
  996.       NXRunAlertPanel("IGS Error", "You must first set your password in the Preferences panel.",
  997.               "OK", 0, 0);
  998.       [prefPanel makeKeyAndOrderFront:self];
  999.       [IGSPassword selectText:self];
  1000.  
  1001.       return self;
  1002.     }
  1003.  
  1004.   sethost((char *)[IGSAddress stringValue]);
  1005.   setport(6969);
  1006.   strcpy(IGSPasswordText, (char *)[IGSPassword stringValue]);
  1007.   strcpy(IGSLoginText, (char *)[IGSLoginName stringValue]);
  1008.   [prefPanel close];
  1009.   
  1010.   if (SmartGoGameFlag)
  1011.     {
  1012.       SmartGoGameFlag = 0;
  1013.       [smartGoPanel close];
  1014.     }
  1015.     
  1016.   [[IGSStatus docView] setText:""];
  1017.   [IGSStatusPanel makeKeyAndOrderFront:self];
  1018.   [IGSStatusPanel setMiniwindowIcon:"NeXTGoFile"];
  1019.   [IGSPanel makeKeyAndOrderFront:self];
  1020.   [IGSPanel setMiniwindowIcon:"NeXTGoFile"];
  1021.   [IGSCommand selectText:self];
  1022.   
  1023.   if (open_connection())
  1024.     {      
  1025.       [self SetIGSStatus:"Unable to make a connection.\n"];
  1026.       gameType = LOCAL;
  1027.  
  1028.       NXRunAlertPanel("IGS Error", "I was unable to make a connection.", "OK", 0, 0);
  1029.       [IGSPanel close];
  1030.       [IGSStatusPanel close];
  1031.  
  1032.       return self;
  1033.     }
  1034.   
  1035.   [self SetIGSStatus:"Connection established."];
  1036.   sprintf(s, "Logging in as %s\n", IGSLoginText);
  1037.   [self SetIGSStatus:s];
  1038.   
  1039.   timer = DPSAddTimedEntry(0.25, &checkingNetTraffic, self, NX_BASETHRESHOLD);
  1040.   
  1041.   gameType = IGSGAME;
  1042.   finished = YES;
  1043.   idle = 1;
  1044.  
  1045.   initparser();
  1046.  
  1047.   return self;
  1048. }
  1049.  
  1050. - checkNetTraffic
  1051. {
  1052.   fd_set readers;
  1053.   struct timeval to;
  1054.   int sel;
  1055.   
  1056.   to.tv_sec = 0;
  1057.   to.tv_usec = 0;
  1058.   
  1059.   FD_ZERO(&readers);
  1060.   FD_SET(sock, &readers);
  1061.   
  1062.   sel = select(sock + 1, &readers, NULL, NULL, &to);
  1063.   if (FD_ISSET(sock, &readers))
  1064.     incomingserver();
  1065.     
  1066.   return self;
  1067. }
  1068.  
  1069. - SetIGSStatus:(char *)s
  1070. {
  1071.   Text *docView = [IGSStatus docView];
  1072.   int dvLen;
  1073.   char retstr[5];
  1074.  
  1075.   strcpy(retstr, "\n");
  1076.  
  1077.   dvLen = [docView textLength];
  1078.   [docView setSel:dvLen :dvLen];
  1079.   [docView setSelFont:IGSfont];
  1080.   [docView replaceSel: s];
  1081.   if (printBold)
  1082.     {
  1083.       [docView setSel:dvLen :[docView textLength]];
  1084.       [docView setSelFont:IGSboldFont];
  1085.       [docView setSelFontStyle:NX_BOLD];
  1086.       printBold = NO;
  1087.     }
  1088.   else
  1089.     [docView setSel:[docView textLength] :[docView textLength]];
  1090.  
  1091.   if (s[strlen(s) - 1] != '\n')
  1092.     {
  1093.       [docView setSel:[docView textLength] :0];
  1094.       [docView replaceSel: retstr];
  1095.       [docView scrollSelToVisible];
  1096.     }
  1097.  
  1098.   [docView scrollSelToVisible];
  1099.  
  1100.   return self;
  1101. }
  1102.  
  1103. - getGoView
  1104. {
  1105.   return MainGoView;
  1106. }
  1107.  
  1108. /*
  1109.   The following methods are the various commands for the Internet Go Server
  1110.   */
  1111.  
  1112. - IGSadjourn:sender
  1113. {
  1114.   IGSSendString("adjourn");
  1115.   IGSSendString("\n");
  1116.  
  1117.   return self;
  1118. }
  1119.  
  1120. - IGSbest:sender
  1121. {
  1122.   IGSSendString("best ");
  1123.   IGSSendString("\n");
  1124.  
  1125.   return self;
  1126. }
  1127.  
  1128. - IGSdecline:sender
  1129. {
  1130.   IGSSendString("decline");
  1131.   IGSSendString("\n");
  1132.  
  1133.   return self;
  1134. }
  1135.  
  1136. - IGSdecrease:sender
  1137. {
  1138.   IGSSendString("decrease");
  1139.   IGSSendString("\n");
  1140.  
  1141.   return self;
  1142. }
  1143.  
  1144. - IGSdone:sender
  1145. {
  1146.   IGSSendString("done");
  1147.   IGSSendString("\n");
  1148.  
  1149.   return self;
  1150. }
  1151.  
  1152. - IGSgames:sender
  1153. {
  1154.   IGSSendString("games ");
  1155.   IGSSendString("\n");
  1156.  
  1157.   return self;
  1158. }
  1159.  
  1160. - IGSincrease:sender
  1161. {
  1162.   IGSSendString("increase");
  1163.   IGSSendString("\n");
  1164.  
  1165.   return self;
  1166. }
  1167.  
  1168. - IGSobserve:sender
  1169. {
  1170.   message mess;
  1171.   NXRect frameRect = {15, 30, 497, 226}, scrollRect = {0, 0, 497, 226};
  1172.   NXSize cellSize = {470, 30};
  1173.   char str[80];
  1174.   int i;
  1175.  
  1176.   idle = 0;
  1177.  
  1178.   getgames(&mess);
  1179.   observeMatrix = [[Matrix alloc] initFrame:&frameRect mode:NX_RADIOMODE
  1180.          cellClass:[ClickCell class] numRows:0 numCols:1];
  1181.   [observeMatrix setCellSize:&cellSize];
  1182.  
  1183.   for (i = 0; i < mess.gamecount; i++)
  1184.     {
  1185.       sprintf(str, "%3d -- %12s [%3s] vs. %12s [%3s] (%3d %d %d %3.1f)",
  1186.           mess.gamelist[i].gnum, mess.gamelist[i].white,
  1187.           mess.gamelist[i].wrank, mess.gamelist[i].black,
  1188.           mess.gamelist[i].brank, mess.gamelist[i].mnum,
  1189.           mess.gamelist[i].bsize, mess.gamelist[i].hcap,
  1190.           mess.gamelist[i].komi);
  1191.       [observeMatrix addRow];
  1192.       observeCell = [observeMatrix cellAt:i :0];
  1193.       [observeCell setStringValue:str];
  1194.       [observeCell setAlignment:NX_LEFTALIGNED];
  1195.     }
  1196.   [observeMatrix sizeToCells];
  1197.  
  1198.   observeScrollView = [[ScrollView alloc] initFrame:&scrollRect];
  1199.   [observeScrollView setVertScrollerRequired:YES];
  1200.   [observeScrollView setBorderType:NX_BEZEL];
  1201.   [observeScrollView setBackgroundGray:NX_LTGRAY];
  1202.   [observeScrollView setDocView:observeMatrix];
  1203.   [observeBox addSubview:observeScrollView];
  1204.   [observeMatrix scrollCellToVisible:0 :0];
  1205.   [observeBox display];
  1206.  
  1207.   [observeSelPanel makeKeyAndOrderFront:self];
  1208.  
  1209.   [IGSCommand selectText:self];
  1210.   idle = 1;
  1211.  
  1212.   return self;
  1213. }
  1214.  
  1215. - cellClicked:theCell
  1216. {
  1217.   char str[80];
  1218.   int n;
  1219.  
  1220.   strcpy(str, [theCell stringValue]);
  1221.   sscanf(str, "%3d", &n);
  1222.  
  1223.   idle = 0;
  1224.   observegame(n);
  1225.   idle = 1;
  1226.  
  1227.   [observeSelPanel close];
  1228.  
  1229.   return self;
  1230. }
  1231.  
  1232. - IGSplayers:sender
  1233. {
  1234.   IGSSendString("players");
  1235.   IGSSendString("\n");
  1236.  
  1237.   return self;
  1238. }
  1239.  
  1240. - IGSquit:sender
  1241. {
  1242.   IGSSendString("quit");
  1243.   IGSSendString("\n");
  1244.   
  1245.   DPSRemoveTimedEntry(timer);
  1246.  
  1247.   gameType = LOCAL;
  1248.   [IGSPanel close];
  1249.   [IGSStatusPanel close];
  1250.  
  1251.   return self;
  1252. }
  1253.  
  1254. - IGSranks:sender
  1255. {
  1256.   IGSSendString("ranks");
  1257.   IGSSendString("\n");
  1258.  
  1259.   return self;
  1260. }
  1261.  
  1262. - IGSrefresh:sender
  1263. {
  1264.   [MainGoView refreshIO];
  1265.  
  1266.   return self;
  1267. }
  1268.  
  1269. - IGSreset:sender
  1270. {
  1271.   IGSSendString("reset");
  1272.   IGSSendString("\n");
  1273.  
  1274.   return self;
  1275. }
  1276.  
  1277. - IGSresign:sender
  1278. {
  1279.   IGSSendString("resign");
  1280.   IGSSendString("\n");
  1281.  
  1282.   return self;
  1283. }
  1284.  
  1285. - IGSsave:sender
  1286. {
  1287.   IGSSendString("save");
  1288.   IGSSendString("\n");
  1289.  
  1290.   return self;
  1291. }
  1292.  
  1293. - IGSstored:sender
  1294. {
  1295.   IGSSendString("stored");
  1296.   IGSSendString("\n");
  1297.  
  1298.   return self;
  1299. }
  1300.  
  1301. - IGSthist:sender
  1302. {
  1303.   IGSSendString("thist ");
  1304.   IGSSendString("\n");
  1305.  
  1306.   return self;
  1307. }
  1308.  
  1309. - IGSuptime:sender
  1310. {
  1311.   IGSSendString("uptime");
  1312.   IGSSendString("\n");
  1313.  
  1314.   return self;
  1315. }
  1316.  
  1317. - IGSwatching:sender
  1318. {
  1319.   IGSSendString("watching");
  1320.   IGSSendString("\n");
  1321.  
  1322.   return self;
  1323. }
  1324.  
  1325. - IGSwho:sender
  1326. {
  1327.   IGSSendString("who");
  1328.   IGSSendString("\n");
  1329.  
  1330.   return self;
  1331. }
  1332.  
  1333. @end
  1334.